package com.videoaggre.zcl.ui.fragment.qiman

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.kingja.loadsir.core.LoadService
import com.videoaggre.zcl.R
import com.videoaggre.zcl.api.ApiObserver
import com.videoaggre.zcl.api.QIMAN_SERVICE
import com.videoaggre.zcl.bean.*
import com.videoaggre.zcl.common.Config
import com.videoaggre.zcl.common.drawable
import com.videoaggre.zcl.common.showLoadingCallback
import com.videoaggre.zcl.common.startKtxActivity
import com.videoaggre.zcl.databinding.QimanItemHomeHotBinding
import com.videoaggre.zcl.databinding.QimanItemHomeNormalBinding
import com.videoaggre.zcl.databinding.QimanItemHomeTitleBinding
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.parse.impl.QimanParse
import com.videoaggre.zcl.ui.activity.qiman.QimanDetailActivity
import com.videoaggre.zcl.ui.activity.qiman.QimanSearchActivity
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import com.videoaggre.zcl.ui.fragment.BaseFragment
import com.videoaggre.zcl.ui.fragment.launchUI
import com.videoaggre.zcl.utils.GlideImageLoader
import com.videoaggre.zcl.utils.GlideUtils
import com.youth.banner.BannerConfig
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import kotlinx.android.synthetic.main.qiman_fragment_home.*

class QimanHomeFragment : BaseFragment(R.layout.qiman_fragment_home) {
    private val mQimanComicPreAdapter: QimanComicHomeAdapter = QimanComicHomeAdapter()
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(rootView) { onReload() } }
    private var mQimanHome: QimanHome? = null
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        mView = mLoadService.loadLayout
        bindViewClick(titleView.rightImageView)
        recyclerView.layoutManager = GridLayoutManager(context, Config.QIMAN_HOME_SPAN_SIZE)
        recyclerView.adapter = mQimanComicPreAdapter
    }

    override fun initData(rootView: View?, savedInstanceState: Bundle?) {
        mLoadService.showLoadingCallback()
        onReload()
    }

    override fun onClick(v: View?) {
        when (v) {
            titleView.rightImageView -> startKtxActivity<QimanSearchActivity>()
        }
    }

    private fun onReload() {
        launchUI({ QimanParse.parseHome(QIMAN_SERVICE.qimanHome()) }, ApiObserver(loadService = mLoadService) {
            handQimanHome(it)
        })
    }

    private fun handQimanHome(qimanHome: QimanHome?) {
        mQimanHome = qimanHome
        qimanHome?.apply {
            viewBanner.run {
                setImageLoader(GlideImageLoader())
                setIndicatorGravity(BannerConfig.RIGHT)
                setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                setOnBannerListener { mQimanHome?.banner?.get(it)?.let { openDetail(it.id) } }
                setDelayTime(2000)
                setImages(banner.map { it.img })
            }.start()
            mQimanComicPreAdapter.datas = item
            mQimanComicPreAdapter.notifyDataSetChanged()
        }
    }

    private fun openDetail(id: String) {
        QimanDetailActivity.openDetail(context, id)
    }

    inner class QimanComicHomeAdapter : BaseRecyclerAdapter<BaseViewHolder<ViewBinding>, QimanItem>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ViewBinding> =
            when (getItem(viewType)) {
                is QimanComicHomeTitle -> BaseViewHolder(QimanItemHomeTitleBinding.inflate(getLayoutInflate(parent.context), parent, false))
                is QimanComicHomeHot -> BaseViewHolder(QimanItemHomeHotBinding.inflate(getLayoutInflate(parent.context), parent, false))
                is QimanComicHomeNormal -> BaseViewHolder(QimanItemHomeNormalBinding.inflate(getLayoutInflate(parent.context), parent, false))
                else -> BaseViewHolder(QimanItemHomeHotBinding.inflate(getLayoutInflate(parent.context), parent, false))
            }

        override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
            (recyclerView.layoutManager as GridLayoutManager).apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int) = getItem(position).spanSize()
                }
            }
        }

        override fun onBindViewHolder(holder: BaseViewHolder<ViewBinding>, bean: QimanItem, position: Int) {
            holder.mViewBinding.also {
                when (it) {
                    is QimanItemHomeTitleBinding -> {
                        it.apply {
                            (bean as QimanComicHomeTitle).apply {
                                ivTitle.run {
                                    val mDrawable = R.mipmap.ic_home_dj.drawable?.apply { setBounds(0, 0, intrinsicWidth, intrinsicHeight) }
                                    setCompoundDrawables(mDrawable, compoundDrawables[1], compoundDrawables[2], compoundDrawables[3])
                                }
                                ivTitle.text = title
                                setViewClick(position, tvMore)
                            }
                        }
                    }
                    is QimanItemHomeHotBinding -> {
                        it.apply {
                            (bean as QimanComicHomeHot).apply {
                                GlideUtils.intoRoundedCorners(ivImage.context, img, ivImage, roundingRadius = 4, cornerType = RoundedCornersTransformation.CornerType.TOP)
                                tvName.text = name
                                tvDescName.text = desc
                            }
                        }
                    }
                    is QimanItemHomeNormalBinding -> {
                        it.apply {
                            (bean as QimanComicHomeNormal).apply {
                                GlideUtils.intoRoundedCorners(ivImage.context, img, ivImage, roundingRadius = 4, cornerType = RoundedCornersTransformation.CornerType.TOP)
                                tvName.text = name
                                tvDescName.text = desc
                                setViewClick(position, root)
                            }
                        }
                    }
                }
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            val item = getItem(position)
            when (item) {
                is QimanComicHomeTitle -> {

                }
                is QimanComicHomeNormal -> openDetail(item.id)
            }
        }
    }
}